package org.example.servlet;

import org.example.dao.StarDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 登录
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        resp.setHeader("expires", "-1");
        resp.setHeader("pragma", "no-cache");
        resp.setHeader("cache-control", "no-cache");

        // ?username=hahha&password=12345
        String user = req.getParameter("Userid");
        String pwd = req.getParameter("Password");

        boolean isLoginSuccess = StarDao.login(user, pwd);
        if (isLoginSuccess){
            resp.sendRedirect("/index.html");
        }else {
            resp.sendRedirect("/page/404.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

